package de.tum.in.eist.im.client.GUIwidgets;

import java.util.Date;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;

import de.tum.in.eist.im.client.GUI;
import de.tum.in.eist.im.shared.Message;

public class Chat extends GUIwidget {
	private TabLayoutPanel tabPanel;
	// Chat widgets (top to bottom)
	private ScrollPanel chatScrollPanel;
	private FlexTable chatLog;

	private TextBox messageBox;
	private Button sendMessageButton;

	public Chat(GUI parent) {
		setGUIandRootPanel(parent);

		// Chat
		tabPanel = new TabLayoutPanel(2,Unit.EM);
		tabPanel.setSize("793px", "510px");
		chatScrollPanel = new ScrollPanel();
		tabPanel.add(chatScrollPanel, "All", false);
		chatScrollPanel.setSize("780px", "471px");
		// DOM.setStyleAttribute(chatScrollPanel.getElement(), "overflowX",
		// "hidden");
		chatLog = new FlexTable();
		chatScrollPanel.setWidget(chatLog);
		chatLog.setSize("750px", "459px");
		chatLog.getColumnFormatter().setWidth(0, "150px");
		chatLog.getColumnFormatter().setWidth(1, "400px");
		chatLog.setCellSpacing(10);

		messageBox = new TextBox();
		messageBox.setSize("694px", "16px");

		sendMessageButton = new Button("send message");
		sendMessageButton.setText("Send");
		sendMessageButton.setSize("83px", "28px");
		
		addHandlersToWidgets();
	}

	public void addHandlersToWidgets() {
		// for sending messages
		class ChatHandler implements ClickHandler, KeyUpHandler {
			public void onClick(ClickEvent event) {
				sendMessage();
			}

			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendMessage();
				}
			}

			public void sendMessage() {
				String messageText = messageBox.getText();
				if (!messageText.equals("")) {
					parentGUI.getParentIM().sendMessage(messageText.trim(), "LoggedIn");
					messageBox.setText("");
				}
			}
		}
		ChatHandler chatHandler = new ChatHandler();
		messageBox.addKeyUpHandler(chatHandler);
		sendMessageButton.addClickHandler(chatHandler);
	}

	public void show() {
		rootPanel.add(tabPanel, 10, 44);
		//rootPanel.add(chatScrollPanel, 10, 80);
		rootPanel.add(messageBox, 10, 560);
		rootPanel.add(sendMessageButton, 720, 560);
	}

	public void hide() {
		rootPanel.remove(tabPanel);
		//rootPanel.remove(chatScrollPanel);
		rootPanel.remove(messageBox);
		rootPanel.remove(sendMessageButton);
	}

	public TextBox getMessageBox() {
		return messageBox;
	}
	
	public void updateAddButtonPosition(int x, int y) {
	}

	@SuppressWarnings("deprecation")
	public void addMessage(Message m) {
		int line = chatLog.getRowCount();
		Date d = new Date(m.getTimestamp());
		String date = "";
		if (d.getHours() < 10) {
			date += "0" + d.getHours();
		} else {
			date += d.getHours();
		}
		date += ":";
		if (d.getMinutes() < 10) {
			date += "0" + d.getMinutes();
		} else {
			date += d.getMinutes();
		}
		date += ":";
		if (d.getSeconds() < 10) {
			date += "0" + d.getSeconds();
		} else {
			date += d.getSeconds();
		}
		chatLog.setText(line, 1, m.getText());
		chatLog.setText(line, 0, date + " " + m.getSender().getUsername());
		chatScrollPanel.scrollToBottom();
	}
}
